Imports System.Data.SqlClient
Public Class cls_alambreesencial_

    Implements ICloneable

#Region "Variables ..."
    Private int_idalambreesencial As Integer = 0
    Private dbl_valordesde As Double = 0
    Private dbl_valorhasta As Double = 0
    Private int_ladofuente As Integer = 0
    Private int_ladopelicula As Integer = 0
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_ = Nothing
    Private col_Items As Collections.Generic.List(Of cls_alambreesencial_) = Nothing
    Public Overrides Function ToString() As String
        Return ladofuente & " - " & ladopelicula
    End Function
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idalambreesencial
        End Get
    End Property
    Property idalambreesencial() As Integer
        Get
            idalambreesencial = int_idalambreesencial
        End Get
        Set(ByVal value As Integer)
            int_idalambreesencial = value
        End Set
    End Property
    Property valordesde() As Double
        Get
            valordesde = dbl_valordesde
        End Get
        Set(ByVal value As Double)
            dbl_valordesde = value
        End Set
    End Property
    Property valorhasta() As Double
        Get
            valorhasta = dbl_valorhasta
        End Get
        Set(ByVal value As Double)
            dbl_valorhasta = value
        End Set
    End Property
    Property ladofuente() As Integer
        Get
            ladofuente = int_ladofuente
        End Get
        Set(ByVal value As Integer)
            int_ladofuente = value
        End Set
    End Property
    Property ladopelicula() As Integer
        Get
            ladopelicula = int_ladopelicula
        End Get
        Set(ByVal value As Integer)
            int_ladopelicula = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_alambreesencial_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_alambreesencial_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_alambreesencial_)
        While obj_Reader.Read()
            Dim Elemento As New cls_alambreesencial_
            Elemento.int_idalambreesencial = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idalambreesencial")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valordesde"))) = False Then
                Elemento.dbl_valordesde = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valordesde")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valorhasta"))) = False Then
                Elemento.dbl_valorhasta = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valorhasta")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ladofuente"))) = False Then
                Elemento.int_ladofuente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ladofuente")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ladopelicula"))) = False Then
                Elemento.int_ladopelicula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ladopelicula")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idalambreesencial = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idalambreesencial")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valordesde"))) = False Then
            dbl_valordesde = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valordesde")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valorhasta"))) = False Then
            dbl_valorhasta = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valorhasta")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ladofuente"))) = False Then
            int_ladofuente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ladofuente")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ladopelicula"))) = False Then
            int_ladopelicula = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ladopelicula")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__Nuevo"
                .Parameters.Add("@valordesde", SqlDbType.Decimal).Value = dbl_valordesde
                .Parameters.Add("@valorhasta", SqlDbType.Decimal).Value = dbl_valorhasta
                .Parameters.Add("@ladofuente", SqlDbType.Int).Value = int_ladofuente
                .Parameters.Add("@ladopelicula", SqlDbType.Int).Value = int_ladopelicula
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__Modificar"
                .Parameters.Add("@idalambreesencial", SqlDbType.Int).Value = int_idalambreesencial
                .Parameters.Add("@valordesde", SqlDbType.Decimal).Value = dbl_valordesde
                .Parameters.Add("@valorhasta", SqlDbType.Decimal).Value = dbl_valorhasta
                .Parameters.Add("@ladofuente", SqlDbType.Int).Value = int_ladofuente
                .Parameters.Add("@ladopelicula", SqlDbType.Int).Value = int_ladopelicula
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idalambreesencial As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__Eliminar"
                .Parameters.Add("@idalambreesencial", SqlDbType.Int).Value = idalambreesencial
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idalambreesencial As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__Buscar"
                .Parameters.Add("@idalambreesencial", SqlDbType.Int).Value = idalambreesencial
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "alambreesencial__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de alambreesencial_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_alambreesencial_Clon As New cls_alambreesencial_
        obj_alambreesencial_Clon.int_idalambreesencial = Me.int_idalambreesencial
        obj_alambreesencial_Clon.dbl_valordesde = Me.dbl_valordesde
        obj_alambreesencial_Clon.dbl_valorhasta = Me.dbl_valorhasta
        obj_alambreesencial_Clon.int_ladofuente = Me.int_ladofuente
        obj_alambreesencial_Clon.int_ladopelicula = Me.int_ladopelicula
        obj_alambreesencial_Clon.col_Items = Me.col_Items
        Return obj_alambreesencial_Clon
    End Function

End Class
